layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let laydate = layui.laydate;

    // $('head link[rel="stylesheet"][href*="/green/pace-theme-center-atom.css"]').remove();

    //对外暴露的接口
    exports('push_inner_remind', {});
});


function loadRemind(id) {
    var $ = layui.$;
    let url = "/push/inner/read.action";
    $.ajax({
        type: "POST", url: url,
        dataType: "json", data: {innerId: id, status:3},
        success: function (json) {
            if (json.status == 200) {
                let dataUrl = "/push/inner/listData.action";
                $.ajax({
                    type: "POST", url: dataUrl,
                    dataType: "json", data: {},
                    success: function (response) {
                        if (response.status == 200) {
                            if (response.data.length > 0) {
                                $(".news-item").remove();
                                let dataHtml = spliceData(response.data);
                                $("#remindTip").append(dataHtml);
                            } else {
                                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index);
                            }
                        }
                    }
                });
            } else {
                layer.msg(json.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

function spliceData(items) {
    let dataHtml = "";
    for (var i = 0; i < items.length; i++) {
        let innerId = items[i].id;
        let linkUrl = items[i].linkUrl;
        let linkName = items[i].linkName;
        let content = items[i].content;
        let createTime = items[i].createTime;
        let realContent = content;
        if (content != null && content.length > 40) {
            realContent = realContent.substr(0, 39) + '... >>';
        }
        let createTimeStr = zmnUtil.createTime(createTime);
        dataHtml += '<div style="background-color: #f2f2f2;margin-left: -10px;margin-right: -10px" class="news-item">\n' +
            '                <div class="layui-form-item" style="margin-bottom: 0px !important; margin-left: 10px">\n' +
            '                    <label onclick="toLandingPage(\''+innerId +'\', '+ '\'' +linkUrl +'\', \''+linkName +'\')" style="font-weight: bold">'+ linkName +'</label>\n' +
            '                    <a onclick="loadRemind('+ innerId +');" class="layui-input-inline layui-input-line-width" style="float: right;cursor: pointer;;margin-bottom: 0px !important;">×</a>\n' +
            '                </div>\n' +
            '                <div class="layui-form-item" style="margin-bottom: 0px !important;margin-left: 10px">\n' +
            '                    <label onclick="toLandingPage(\''+innerId +'\', '+ '\'' +linkUrl +'\', \''+linkName +'\')">'+ realContent +'</label>\n' +
            '                    <div class="layui-input-inline layui-input-line-width" style="float: right;margin-bottom: 0px !important;"\n' +
            '                     >'+ createTimeStr +'</div>\n' +
            '                </div>\n' +
            '            </div>';
    }

    return dataHtml;
}

function toLandingPage(id, landingPage, title) {
    var $ = layui.$;
    let url = "/push/inner/read.action";
    $.ajax({
        type: "POST", url: url,
        dataType: "json", data: {innerId: id, status:2},
        success: function (json) {
            if (json.status == 200) {
                zmnLayUtil.openTabsPageCustom(parent, landingPage, title);
                handleMsgCountAndRefreshLayer();
            } else {
                layer.msg(json.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

function handleMsgCountAndRefreshLayer() {
    let countUrl = "/push/inner/waitReadMsgCount.action";
    $.ajax({
        type: "POST", url: countUrl,
        dataType: "json", data: {},
        success: function (items) {
            if (items.status == 200) {
                if (items.data == 0) {
                    $(top.window.document.body).find('#unReadMsgCount').hide();
                } else if (items.data <= 99) {
                    $(top.window.document.body).find('#unReadMsgCount').text(items.data);
                    $(top.window.document.body).find('#unReadMsgCount').show();
                } else {
                    $(top.window.document.body).find('#unReadMsgCount').text("99+");
                    $(top.window.document.body).find('#unReadMsgCount').show();
                }

                if (items.data > 0) {
                    let dataUrl = "/push/inner/listData.action";
                    $.ajax({
                        type: "POST", url: dataUrl,
                        dataType: "json", data: {},
                        success: function (response) {
                            if (response.status == 200) {
                                if (response.data.length > 0) {
                                    $(".news-item").remove();
                                    let dataHtml = spliceData(response.data);
                                    $("#remindTip").append(dataHtml);
                                } else {
                                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index);
                                }
                            } else {
                                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index);
                            }
                        }
                    });
                } else {
                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                }
            }
        }
    });
}